﻿@model SiteCredivale.ViewModels.documentosViewModel
@using Domain.Entidades;
@{

    Layout = "~/Areas/Intranet/Views/Shared/_Layout2.cshtml";

}

<script type="text/javascript" src="~/Areas/Intranet/ckeditor/ckeditor.js"></script>
<div id="topico" class="topico">@ViewBag.titulo</div>
<div class="box_medio">

    @using (Html.BeginForm(null, null, FormMethod.Post, new { @id = "form", enctype = "multipart/form-data" }))
    {


        @Html.ValidationSummary(true)

        @Html.HiddenFor(x => x.doc_codigo)
        <div class="editor-field">
            <label class="txtLabel">
                Origem:
            </label>
            @Html.EditorFor(x => x.tipos_documentos.origem_documentos.ori_descricao, new { htmlAttributes = new { @readonly = "readonly" } })

        </div>
        <br />

        <div class="editor-field">
            <label class="txtLabel">
                Tipo:
            </label>

            @Html.EditorFor(x => x.tipos_documentos.tip_descricao, new { htmlAttributes = new { @readonly = "readonly" } })

        </div>

        <br />


        <div class="editor-field">
            <label class="txtLabel">Arquivo:</label>
            @Html.TextAreaFor(x => x.doc_desc, new { @readonly = true })
        </div>
        <br />


        <input type="hidden" name="jsusuarios" id="jsusuarios" value="" />

        <fieldset id="documentos" name="documentos">
            <legend class="editor-field">
                <label class="txtLabel">
                    Usuários que visualizarão:
                </label>
            </legend>
            <br />

            <div id="demoTree"></div>

        </fieldset> @Html.ValidationMessageFor(model => model.jsusuarios)

       <input id="Salvar" type="button" value="Liberar/Revogar" class="btn" />

    }


    @if (ViewBag.res != null)
    {
        @Html.Raw("<script type='text/javascript'>successAlert('Sucesso', '" + ViewBag.res + "');</script>")
    }
    @if (ViewBag.err != null)
    {
        @Html.Raw("<script type='text/javascript'>errorsAlert('Atenção', '" + ViewBag.err + "');</script>")
    }
    <script src="@Url.Content("~/Scripts/jquery.jstree.js")" type="text/javascript"></script>
    <script>




        function pega_usuarios() {
            var checked_ids = [];
            $('#demoTree').jstree("get_checked",null,true).each(function(){
                checked_ids.push(this.id);
            });
            //setting to hidden field
            document.getElementById('jsusuarios').value = checked_ids.join(",");
        }

        $().ready(function () {




            treeModel = @Html.Raw(ViewBag.arvore);

                $("#demoTree").jstree({
                    json_data: {
                        data: treeModel
                    },
                    themes: {
                        theme: "default",
                        icons: false,
                        url: "/Scripts/themes/default/style.css",
                        variant: "apple"
                    },
                    checkbox: {
                        real_checkboxes: true,
                        real_checkboxes_names: function (n) {
                            return [("check_" + (n[0].id || Math.ceil(Math.random() * 10000))), n[0].id]
                        }
                    },
                    plugins: ["themes","json_data", "ui","checkbox"]
                }).bind("loaded.jstree", function (event, data) {

                    $('#demoTree').jstree('open_node','#todos');
                    $('#demoTree').jstree('check_node', 'li[selected=selected]');
                });

        });
                var myFile = document.getElementById('Salvar');
                myFile.addEventListener('click', function () {
                   

                        pega_usuarios();
                        $("#form").submit();

        });
    </script>
</div>